package player

import (
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
)

// 12050 根据IdentifyID，获取用户信息
func (p *Player) GetUserByIdentifyIDRequest(req *sp.GetUserByIdentifyIDRequest) (res *sp.GetUserByIdentifyIDResponse) {
	uid, err := lib.FindUidByIdentifId(req.IdentifId)
	if err != nil {
		res = &sp.GetUserByIdentifyIDResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	tu, ok := PlayersModule.GetUser(uid)
	if !ok {
		res = &sp.GetUserByIdentifyIDResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	res = &sp.GetUserByIdentifyIDResponse{
		UserData: lib.FormatSimpleFriend(tu),
	}
	return
}

// 12051 获取推荐好友列表
func (p *Player) GetActiveUserLstRequest(req *sp.GetActiveUserLstRequest) (res *sp.GetActiveUserLstResponse) {
	friendUids := lib.GetFriendsUidLst(p.User)
	// uids := active_user.RecomFriend(p.User)
	var fPageT int = 0
	if fPage, ok := p.GetData("fPage"); ok {
		fPageT = fPage.(int)
	}
	if req.Refresh == 1 { // 刷新数据，查找下一页
		fPageT++
	}
	uids := lib.RecomFriend(p.User, fPageT, 30, friendUids)
	if len(uids) == 0 { // 查询的数据为0，表示查找数据末尾，分页其实位置重置为0
		fPageT = 0
		uids = lib.RecomFriend(p.User, fPageT, 30, friendUids) // 分页重置为0，再查询一次
		// 这时候没有数据，就真的没有数据
	}
	p.PutData("fPage", fPageT)

	lst := make([]*m.User, 0)
	for _, item := range uids {
		if tu, ok := PlayersModule.GetUser(item); ok {
			lst = append(lst, tu)
		}
	}
	res = &sp.GetActiveUserLstResponse{
		UserData: lib.FormatSimpleFriendLst(lst),
	}
	return
}
